﻿using System.Collections.Generic;
using System.Linq;
using BankCreditPortfolio.DAL.Entities;

namespace BankCreditPortfolio.DAL.Services
{
    public class OrderHistoryService : DataService<OrderHistoryPersistence>
    {
        private Tariff GetTariffObject(TariffPersistence obj)
        {
            var result = new Tariff
            {
                TariffId = obj.TariffId,
                CreditPercent = obj.CreditPercent,
                Currency = obj.Currency,
                Description = obj.Description,
                FirstPayment = obj.FirstPayment,
                Max = obj.Max,
                Min = obj.Min,
                NumberOfGuarantors = obj.NumberOfGuarantors,
                OtherPayments = obj.OtherPayments,
                Time = obj.Time,
                Title = obj.Title,
                Fine = obj.Fine,
                EarlyRepaymentFine = obj.EarlyRepaymentFine,
                Used = obj.Used
            };
            return result;
        }

        private OrderInfo GetOrderInfoObject(OrderInfoPersistence obj)
        {
            var res = new OrderInfo
            {
                Currency = obj.Currency,
                Documents = obj.Documents,
                Gains = obj.Gains,
                //Guarantor = obj.Guarantor,
                OrderId = obj.OrderId,
                OrderStatus = obj.OrderStatus,
                Tariff = GetTariffObject(obj.Tariff),
                Client = obj.Client,
                Amount = obj.Amount,
                GainsCurrency = obj.GainsCurrency
            };
            return res;
        }

        private OrderHistory GetOrderHistoryObject(OrderHistoryPersistence obj)
        {
            var tmp = new OrderHistory
            {
                CommentsCreditCommittieMember = obj.CommentsCreditCommittieMember,
                CommentsCustomerEngineer = obj.CommentsCustomerEngineer,
                CommentsOperationsAnalyst = obj.CommentsOperationsAnalyst,
                CommentsSafetyOfficer = obj.CommentsSafetyOfficer,
                CreditCommittieMember = obj.CreditCommittieMember,
                CustomerEngineer = obj.CustomerEngineer,
                DateCreditCommittieMember = obj.DateCreditCommittieMember,
                DateCustomerEngineer = obj.DateCustomerEngineer,
                DateOperationsAnalyst = obj.DateOperationsAnalyst,
                DateSafetyOfficer = obj.DateSafetyOfficer,
                OperationsAnalyst = obj.OperationsAnalyst,
                OrderHistoryId = obj.OrderHistoryId,
                SafetyOfficer = obj.SafetyOfficer,
                OrderInfo = GetOrderInfoObject(obj.OrderInfo)
            };
            return tmp;
        }

        new public OrderHistory Find(int id)
        {
            return GetOrderHistoryObject(base.Find(id));
        }

        new public IEnumerable<OrderHistory> GetAll()
        {
            return base.GetAll().Select(GetOrderHistoryObject).ToList();
        }

        public override bool Save(ref OrderHistoryPersistence entity)
        {
            var obj = GetOrderHistoryObject(entity);
            var result = this.Save(ref obj);
            entity.OrderHistoryId = obj.OrderHistoryId;
            return result;
        }

        public bool Save(ref OrderHistory entity)
        {
            var obj = new OrderHistoryPersistence
            {
                CommentsCreditCommittieMember = entity.CommentsCreditCommittieMember,
                CommentsCustomerEngineer = entity.CommentsCustomerEngineer,
                CommentsOperationsAnalyst = entity.CommentsOperationsAnalyst,
                CommentsSafetyOfficer = entity.CommentsSafetyOfficer,
                CreditCommittieMember = entity.CreditCommittieMember,
                CustomerEngineer = entity.CustomerEngineer,
                DateCreditCommittieMember = entity.DateCreditCommittieMember,
                DateCustomerEngineer = entity.DateCustomerEngineer,
                DateOperationsAnalyst = entity.DateOperationsAnalyst,
                DateSafetyOfficer = entity.DateSafetyOfficer,
                OperationsAnalyst = entity.OperationsAnalyst,
                OrderHistoryId = entity.OrderHistoryId,
                SafetyOfficer = entity.SafetyOfficer,
                OrderInfo = new OrderInfoPersistence
                {
                    Currency = entity.OrderInfo.Currency,
                    Documents = entity.OrderInfo.Documents,
                    Gains = entity.OrderInfo.Gains,
                    //Guarantor = entity.Order.Guarantor,
                    OrderId = entity.OrderInfo.OrderId,
                    OrderStatus = entity.OrderInfo.OrderStatus,
                    Client = entity.OrderInfo.Client,
                    Amount = entity.OrderInfo.Amount,
                    GainsCurrency = entity.OrderInfo.GainsCurrency,
                    Tariff = new TariffPersistence
                    {
                        CreditPercent = entity.OrderInfo.Tariff.CreditPercent,
                        Currency = entity.OrderInfo.Tariff.Currency,
                        Description = entity.OrderInfo.Tariff.Description,
                        FirstPayment = entity.OrderInfo.Tariff.FirstPayment,
                        Max = entity.OrderInfo.Tariff.Max,
                        Min = entity.OrderInfo.Tariff.Min,
                        NumberOfGuarantors =
                            entity.OrderInfo.Tariff.NumberOfGuarantors,
                        OtherPayments = entity.OrderInfo.Tariff.OtherPayments,
                        Time = entity.OrderInfo.Tariff.Time,
                        Title = entity.OrderInfo.Tariff.Title,
                        Fine = entity.OrderInfo.Tariff.Fine,
                        TariffId = entity.OrderInfo.Tariff.TariffId,
                        EarlyRepaymentFine = entity.OrderInfo.Tariff.EarlyRepaymentFine,
                        Used = entity.OrderInfo.Tariff.Used
                    }
                }
            };
            var res = base.Save(ref obj);
            entity.OrderHistoryId = obj.OrderHistoryId;
            return res;
        }

        new public IQueryable<OrderHistory> CreateQuery()
        {
            var tmpCollection1 = base.CreateQuery();
            var tmpCollection = new List<OrderHistory>();
            foreach (var orderHistoryPersistence in tmpCollection1)
            {
                tmpCollection.Add(GetOrderHistoryObject(orderHistoryPersistence));
            }
            return tmpCollection.AsQueryable();
        }
    }
}